home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / mskrmsrc.zip / MSNLIB.C < prev    next >
C/C++ Source or Header  |  1991-10-24  |  7KB  |  317 lines

  1. /* File MSNLIB.C
  2.  * Replacement for C library for use with MS-DOS Kermit.
  3.  *
  4.  * Copyright (C) 1991, Trustees of Columbia University in the
  5.  *  City of New York.  Permission is granted to any individual or
  6.  *  institution to use, copy, or redistribute this software as long as
  7.  *  it is not sold for profit and this copyright notice is retained.
  8.  *
  9.  * Last edit 6 Sept 1991
  10.  *  Authors: J.R.Doupnik, USU, Frank da Cruz, Columbia Univ.
  11.  * Contains:
  12.  * strchr, strcat, strncat, strcpy, strncpy, strlen, strcmp, stricmp, strncmp
  13.  * atoi, itoa, ltoa, isdigit, ntoa.
  14. */
  15. #include "msnlib.h"
  16.  
  17. #ifndef MSDOS
  18. /*
  19.  In MS-DOS Kermit, these are assembler routines to avoid math library.
  20. */
  21. #define ourmod(a,b)   (a % b)
  22. #define ourdiv(a, b)  (a / b)
  23. #define ourlmod(a,b)  (a % b)
  24. #define ourldiv(a, b) (a / b)
  25. #endif /* MSDOS */
  26.  
  27. #ifndef NULL
  28. #define NULL 0
  29. #endif /* NULL */
  30.  
  31. /*
  32.  By the way, there is probably no point in the next #ifndef,
  33.  because size_t is either built into to the compiler or typedef'd,
  34.  rather than defined.  So the #define below will always happen.
  35. */
  36. #ifndef size_t
  37. #define size_t int
  38. #endif /* size_t */
  39.  
  40. int _acrtused;            /* MS C compiler startup file quantity */
  41.  
  42. /*
  43.   _strchr
  44.   Finds first occurence of character c in string s.
  45.   Returns pointer to it if found, NULL if not found.
  46. */
  47. byte *
  48. strchr(byte *s, const byte c) {
  49.     while ((*s != '\0') && (*s != (c & 0xff))) s++;
  50.     if (*s == '\0') return(NULL);
  51.     else return(s);
  52. }
  53.  
  54. /*
  55.   _strcat
  56.   Appends entire string s2 to string s1.
  57.   Assumes there is room for s2 after end of s1.
  58.   Returns pointer to s1 or NULL if s1 is a null pointer.
  59. */
  60. byte *
  61. strcat(byte *s1, byte *s2) {
  62.     register byte *p;
  63.  
  64.     if (s1 == NULL) return(NULL);
  65.     if (s2 == NULL || *s2 == '\0') return(s1);
  66.     p = s1;                /* Make copy */
  67.     while (*p) p++;            /* Find end */
  68.     while (*p++ = *s2++) ;        /* Copy thru terminating NUL */
  69.     return(s1);                /* Return original */
  70. }
  71.  
  72. /*
  73.   _strncat
  74.   Appends up to n chars of string s2 to string s1.
  75.   Returns pointer to string1 or NULL if s1 is a null pointer.
  76. */
  77. byte *
  78. strncat(byte *s1, byte *s2, size_t n) {
  79.     register byte * p;
  80.  
  81.     if (s1 == NULL) return(NULL);
  82.     if (s2 == NULL || *s2 == '\0') return(s1);
  83.     p = s1;                /* Copy pointer */
  84.     while (*p) p++;            /* Step to end of s1 */
  85.     while ((*p++ = *s2++) && (--n > 0)) ; /* Copy up to n bytes of s2 */
  86.     return(s1);                /* Return original pointer */
  87. }
  88.  
  89. /*
  90.   _strcpy
  91.   Copies s2 to s1, returns pointer to s1 or NULL if s1 was NULL.
  92. */
  93. byte *
  94. strcpy(byte *s1, byte *s2) {
  95.     register byte *p;
  96.  
  97.     if (s1 == NULL) return(NULL);
  98.     if (s2 == NULL) s2 = "";
  99.     p = s1;                /* Copy pointer */
  100.     while (*p++ = *s2++) ;        /* Copy thru terminating NUL */
  101.     return(s1);                /* Return original pointer */
  102. }
  103.  
  104. /*
  105.   _strncpy
  106.   Copies at most n characters from s2 to to s1, returns pointer to s1.
  107.   Returns s1 or NULL if s1 was NULL.
  108. */
  109. byte *
  110. strncpy(byte *s1, byte *s2, size_t n) {
  111.     register int s2len;
  112.     register byte *p1;
  113.  
  114.     if (s1 == NULL) return(NULL);
  115.     if (s2 == NULL) s2 = "";
  116.     if ((s2len = strlen(s2)) > n) s2len = n;
  117.     p1 = s1;
  118.  
  119.     while (s2len-- > 0)            /* Copy */
  120.       *p1++ = *s2++;
  121.     *p1 = '\0';                /* Terminate */
  122.     return(s1);                /* No need to pad out, one's enuf */
  123. }
  124.  
  125. /*
  126.   _strlen
  127.   Returns length of null-terminated string not including '\0'
  128. */
  129. size_t
  130. strlen(byte *s) {
  131.     register int i = 0;
  132.  
  133.     if (s == NULL) return(0);
  134.     while (*s++) i++;
  135.     return(i);
  136. }
  137.  
  138. /*
  139.   _strcmp
  140.   Compare null-terminated strings using ASCII values.
  141.   Case matters.  Returns:
  142.   < 0 if s1 < s2,
  143.   = 0 if s1 = s2,
  144.   > 0 if s1 > s2
  145. */
  146. int
  147. strcmp(byte *s1, byte *s2) {
  148.     if (s1 == NULL) s1 = "";
  149.     if (s2 == NULL) s2 = "";
  150.     do {
  151.     if (*s1 < *s2) return(-1);
  152.     if (*s1 > *s2) return(1);
  153.     if (*s2 == '\0') return(0);
  154.     s2++;
  155.     } while (*s1++);
  156.     return(0);
  157. }
  158.  
  159. /*
  160.   _stricmp
  161.   Like strcmp but case insenstive
  162. */
  163. int
  164. stricmp(byte *s1, byte *s2) {
  165.     register byte c1, c2;
  166.  
  167.     if (s1 == NULL) s1 = "";
  168.     if (s2 == NULL) s2 = "";
  169.     do {
  170.     c1 = *s1; c2 = *s2;
  171.     if ('a' <= c1 && c1 <= 'z') c1 = c1 -'a' + 'A';
  172.     if ('a' <= c2 && c2 <= 'z') c2 = c2 -'a' + 'A';
  173.     if (c1 < c2) return(-1);
  174.     if (c1 > c2) return(1);
  175.     if (c2 == '\0') return(0);
  176.     s1++; s2++;
  177.     } while (c1 != '\0');
  178.     return(0);
  179. }
  180.  
  181. /*
  182.   _strncmp
  183.   Compares at most n characters of strings s1 and s2.
  184. */
  185. int
  186. strncmp(byte *s1, byte *s2, size_t n) {    
  187.  
  188.     if (s1 == NULL) s1 = "";
  189.     if (s2 == NULL) s2 = "";
  190.     while (n-- > 0 && *s1) {
  191.     if (*s1 < *s2) return(-1);
  192.     if (*s1 > *s2) return(1);
  193.     s1++; s2++;
  194.     }
  195.     return(0);
  196. }
  197.  
  198. /*
  199.   _atoi
  200.   Converts decimal numeric string to integer.
  201.   Breaks on first non-digit or end of string.
  202.   Returns integer.
  203. */
  204. int
  205. atoi(byte *s) {
  206.     register int i, count;
  207.     count = 0;
  208.     for (i = 0; i < 18; i++) {
  209.     if (*s < '0' || *s > '9') break;
  210.     count *= 10;
  211.     count += *s - '0';        /* ascii to binary */
  212.     s++;
  213.     }
  214.     return(count);
  215. }
  216.  
  217. /*
  218.   _itoa
  219.   Converts integer value to ASCII digits (up to 18 characters long),
  220.   stores in string, null terminated.  Returns NULL on failure,
  221.   pointer to result on success.
  222. */
  223. byte *
  224. itoa(int value, byte *string, int radix) { /* From K & R */
  225.     int c, j, sign;
  226.     register int i;
  227.     register byte *s;
  228.  
  229.     if (string == NULL) return(NULL);
  230.     
  231.     s = string;
  232.  
  233.     if ((sign = value) < 0)        /* Save sign */
  234.       value = - value;            /* Force value positive */
  235.     i = 0;
  236.     do {
  237.     s[i++] = ourmod(value, radix) + '0';
  238.     } while ((value = ourdiv(value, radix)) > 0);
  239.  
  240.     if (sign < 0)
  241.       s[i++] = '-';
  242.     s[i] = '\0';
  243.     j = strlen(s) -1;
  244.     for (i = 0; i < j; i++, j--) {
  245.     c = s[i];
  246.     if (c > '9') c = c - '9' + 'A' -1;
  247.     s[i] = s[j];
  248.     s[j] = c;
  249.     }
  250.     return(string);
  251. }
  252.  
  253. /*
  254.   _ltoa (in pcdbug.c)
  255.   Like itoa() but using long value ( < 34 ).
  256. */
  257. byte *
  258. ltoa(long value, byte *string, int radix) { /* K & R */
  259.     int c, j;
  260.     register int i;
  261.     long sign;
  262.     register byte * s;
  263.  
  264.     if (string == NULL) return(NULL);
  265.     s = string;
  266.  
  267.     if ((sign = value) < 0) value = - value; /* value to positive*/
  268.     i = 0;
  269.     do {
  270.     s[i++] = ourlmod(value, radix) + '0';
  271.     } while ((value = ourldiv(value, radix)) > 0);
  272.  
  273.     if (sign < 0)
  274.       s[i++] = '-';
  275.     s[i] = '\0';
  276.  
  277.     j = strlen(s) - 1;
  278.     for (i = 0; i < j; i++, j--) {
  279.     c = s[i];
  280.     if (c > '9') c = c - '9' + 'A' -1;
  281.     s[i] = s[j];
  282.     s[j] = c;
  283.     }
  284.     return(string);
  285. }
  286.  
  287. /*
  288.   _isdigit
  289.   Returns 1 if argument is a decimal digit, 0 otherwise.
  290. */
  291. int
  292. isdigit(const byte c) {
  293.     if ((c & 0xff) < '0' || (c & 0xff) > '9')
  294.       return(0);            /* say is not a digit */
  295.     return(1);
  296. }
  297.  
  298. /* 
  299.    Convert long val to dotted decimal string at pointer p, does high order
  300.    byte first. Intended to yield dotted decimal IP addresses from longs.
  301. */
  302. void
  303. ntoa(byte *p, unsigned long val)
  304. {
  305.     register byte *ptr;
  306.     register int i;
  307.     
  308.     ptr = p;
  309.     for (i = 24; i >= 0; i -= 8)
  310.         {
  311.         itoa((int)((val >> i) & 0xff), ptr, 10); /* convert a byte */
  312.         strcat(ptr, ".");            /* dot separator */
  313.         ptr = p + strlen(p);
  314.         }
  315.     *(--ptr) = '\0';            /* remove trailing dot */
  316. }
  317.